﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fideeraSoft.Entities;
using fideeraSoft.Utils;
using System.Data;
using System.Drawing;
using fideeraSoft.Handlers;

namespace fideeraSoft.Paneles
{
    class PanelAddLoteARemito:PanelGeneral
    {
        private Button btVolver;
        private TableLayoutPanel tlpBotones;
        private GroupBox gbGral;
        private TableLayoutPanel tlpGral;
        private Label lbId;
        private Label lbIdLote;
        private Button btGuardarCambios;
        private Form formR;
        private ListBox lsbListBoxLotes;
        private Lote lote;
        private List<Lote> lstLote;

        private System.Data.SqlClient.SqlDataAdapter adapter;
        private DataGridView dgvLotes;
        private DataSet ds;
        private TableLayoutPanel tlpBuscarMateriaPrima;
        private Button btBuscarMateriaPrima;
        private TextBox tbBuscarMateriaPrima;
        private int locationBtCambios;
        private int locationGbBox;
        private int inLocationBuscarMat;

        private Button btAgregarMateriaPrima;
        private TableLayoutPanel tlpAddCantMatPrima;
        private TextBox tbCant;
        private ListBox lsbMatPrima;
        private Button btBorrarItem;
        private TableLayoutPanel tlpLote;
        private DateTimePicker dtFechaVenc;
        private Label lbCod;
        private TextBox tbCod;
        private Label lbFechaVenc;
        private Label lbCant;

        public PanelAddLoteARemito(Form form,ListBox lsbLBoxLotes,List<Lote> lstLot)
        {

            this.formR = form;
            lsbListBoxLotes = lsbLBoxLotes;
            
            //lote = new Lote();
            lstLote = lstLot;

            InitializeComponent();

            this.formatearPanelSegunForm(formR);
            this.formatearTableTitulo("Agregar Lote a Remito");

            string query = "SELECT * FROM MateriaPrima";
            adapter = SQLclass.obtenerAdapter(query);
            if (adapter != null)
            {

                ds = new DataSet();
                adapter.Fill(ds);

                dgvLotes.DataSource = ds.Tables[0];


                this.formatearDataGrid();
                
                this.formatearTablaMateriaPrima();
                
                this.formatearTablaAddCantMateriaPrima();
                
               // this.formatearTablaAgregarMateriaPrima();
                
                this.formatearTablaGral();
                this.formatearTablaLote();

                this.formatearGroupBox();
                
                
                this.formatearTablaBotones();


                this.Controls.Add(dgvLotes);
                
                this.Controls.Add(tlpBuscarMateriaPrima);
                this.Controls.Add(gbGral);
                


                this.Controls.Add(tlpBotones);
            }
            else
            {
                MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.formR.Close();
            }


        }


        #region FORMATEAR

        private void formatearDataGrid()
        {
            dgvLotes.AutoGenerateColumns = true;
            dgvLotes.Size = new Size(this.Size.Width - 100, InitialRealPointVertical);
            dgvLotes.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvLotes.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvLotes.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvLotes.BorderStyle = BorderStyle.Fixed3D;

            dgvLotes.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvLotes.Size.Width), this.InitialRealPointVertical);
        }

        private void formatearTablaMateriaPrima()
        {

            inLocationBuscarMat = this.InitialRealPointVertical + dgvLotes.Size.Height + 20;
            tlpBuscarMateriaPrima.Location = new Point(MetodosGenerales.calcularCenterWidth(this.formR.Size.Width,tlpBuscarMateriaPrima.Size.Width), inLocationBuscarMat);

            tlpBuscarMateriaPrima.Controls.Add(btBuscarMateriaPrima);
            tlpBuscarMateriaPrima.Controls.Add(tbBuscarMateriaPrima);
        }

        private void formatearTablaAddCantMateriaPrima()
        {

            tlpAddCantMatPrima.Controls.Add(btAgregarMateriaPrima);
            tlpAddCantMatPrima.Controls.Add(tbCant);

            tlpAddCantMatPrima.Controls.Add(lbCant);
            
            tlpAddCantMatPrima.Location = new Point(MetodosGenerales.calcularCenterWidth(tlpGral.Size.Width, tlpAddCantMatPrima.Size.Width), 0);
          
        }        

        private void formatearTablaGral()
        {


            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpGral.Size.Width), tlpLote.Size.Height+40);

           // tlpGral.Controls.Add(tlpBuscarMateriaPrima);

            //tlpGral.Controls.Add(tlpAgregarMatPrima);
            tlpGral.Controls.Add(tlpAddCantMatPrima);
            tlpGral.Controls.Add(lsbMatPrima);
            tlpGral.Controls.Add(btBorrarItem);

        }
        private void formatearTablaLote()
        {

            Handler handler = new Handler();
            tlpLote.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpLote.Size.Width), 30);

            if (lsbListBoxLotes.Items.Count == 0)
            {
                lbIdLote.Text = handler.buscarNuevoUltimoIdLoteEnBD().ToString();
            }
            else
            {
                string strValue = lsbListBoxLotes.Items[lsbListBoxLotes.Items.Count - 1].ToString();

                int i = Convert.ToInt32(strValue.Substring(9, strValue.Length-9));
                i++;
                lbIdLote.Text = i.ToString();

            }


            tlpLote.Controls.Add(lbId);
            tlpLote.Controls.Add(lbIdLote);


            tlpLote.Controls.Add(lbCod);
            tlpLote.Controls.Add(tbCod);

            tlpLote.Controls.Add(lbFechaVenc);

            dtFechaVenc.Value = System.DateTime.Now;

            tlpLote.Controls.Add(dtFechaVenc);

            
        }

        private void formatearGroupBox()
        {
            gbGral.Controls.Add(tlpLote);
            gbGral.Controls.Add(tlpGral);
            

            locationGbBox = inLocationBuscarMat+tlpBuscarMateriaPrima.Size.Height + 20;
            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.formR.Size.Width, gbGral.Size.Width), locationGbBox);
                      
        }



        private void formatearTablaBotones()
        {                       
            locationBtCambios = locationGbBox + gbGral.Size.Height + 20;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btGuardarCambios);

        }

        #endregion

        #region INICIALIZAR COMPONENTES
        private void InitializeComponent()
        {
            this.btVolver = new System.Windows.Forms.Button();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btGuardarCambios = new System.Windows.Forms.Button();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.lbId = new System.Windows.Forms.Label();
            this.lbIdLote = new System.Windows.Forms.Label();
            this.dgvLotes = new System.Windows.Forms.DataGridView();
            this.tlpBuscarMateriaPrima = new System.Windows.Forms.TableLayoutPanel();
            this.btBuscarMateriaPrima = new System.Windows.Forms.Button();
            this.tbBuscarMateriaPrima = new System.Windows.Forms.TextBox();
            this.btAgregarMateriaPrima = new System.Windows.Forms.Button();
            this.tlpAddCantMatPrima = new System.Windows.Forms.TableLayoutPanel();
            this.tbCant = new System.Windows.Forms.TextBox();
            this.lbCant = new System.Windows.Forms.Label();
            this.lsbMatPrima = new System.Windows.Forms.ListBox();
            this.btBorrarItem = new System.Windows.Forms.Button();
            this.tlpLote = new System.Windows.Forms.TableLayoutPanel();
            this.dtFechaVenc = new System.Windows.Forms.DateTimePicker();
            this.lbCod = new System.Windows.Forms.Label();
            this.tbCod = new System.Windows.Forms.TextBox();
            this.lbFechaVenc = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dgvLotes)).BeginInit();
            this.SuspendLayout();
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btGuardarCambios
            // 
            this.btGuardarCambios.BackColor = System.Drawing.Color.AliceBlue;
            this.btGuardarCambios.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btGuardarCambios.Location = new System.Drawing.Point(0, 0);
            this.btGuardarCambios.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btGuardarCambios.Name = "btGuardarCambios";
            this.btGuardarCambios.Size = new System.Drawing.Size(75, 40);
            this.btGuardarCambios.TabIndex = 0;
            this.btGuardarCambios.Text = "Agregar";
            this.btGuardarCambios.UseVisualStyleBackColor = false;
            this.btGuardarCambios.Click += new System.EventHandler(this.btGuardarCambios_Click);
            // 
            // gbGral
            // 
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(480, 460);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Lote";
            // 
            // tlpGral
            // 
            this.tlpGral.BackColor = System.Drawing.Color.LightCyan;
            this.tlpGral.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpGral.ColumnCount = 1;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 3;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 13F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 74F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 13F));
            this.tlpGral.Size = new System.Drawing.Size(350, 300);
            this.tlpGral.TabIndex = 0;
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(0, 0);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(100, 23);
            this.lbId.TabIndex = 0;
            this.lbId.Text = "ID";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbIdLote
            // 
            this.lbIdLote.AutoSize = true;
            this.lbIdLote.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdLote.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdLote.Location = new System.Drawing.Point(0, 0);
            this.lbIdLote.Name = "lbIdLote";
            this.lbIdLote.Size = new System.Drawing.Size(100, 23);
            this.lbIdLote.TabIndex = 0;
            this.lbIdLote.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // dgvLotes
            // 
            this.dgvLotes.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvLotes.Location = new System.Drawing.Point(0, 0);
            this.dgvLotes.Name = "dgvLotes";
            this.dgvLotes.ReadOnly = true;
            this.dgvLotes.Size = new System.Drawing.Size(240, 150);
            this.dgvLotes.TabIndex = 0;
            this.dgvLotes.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvLotes_CellContentClick);
            // 
            // tlpBuscarMateriaPrima
            // 
            this.tlpBuscarMateriaPrima.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBuscarMateriaPrima.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarMateriaPrima.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarMateriaPrima.ColumnCount = 2;
            this.tlpBuscarMateriaPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMateriaPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarMateriaPrima.Name = "tlpBuscarMateriaPrima";
            this.tlpBuscarMateriaPrima.RowCount = 1;
            this.tlpBuscarMateriaPrima.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarMateriaPrima.Size = new System.Drawing.Size(400, 40);
            this.tlpBuscarMateriaPrima.TabIndex = 0;
            // 
            // btBuscarMateriaPrima
            // 
            this.btBuscarMateriaPrima.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarMateriaPrima.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarMateriaPrima.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.btBuscarMateriaPrima.Name = "btBuscarMateriaPrima";
            this.btBuscarMateriaPrima.Size = new System.Drawing.Size(200, 30);
            this.btBuscarMateriaPrima.TabIndex = 0;
            this.btBuscarMateriaPrima.Text = "Buscar Materia Prima (nombre)";
            this.btBuscarMateriaPrima.UseVisualStyleBackColor = false;
            this.btBuscarMateriaPrima.Click += new System.EventHandler(this.btBuscarMateriaPrima_Click);
            // 
            // tbBuscarMateriaPrima
            // 
            this.tbBuscarMateriaPrima.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tbBuscarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarMateriaPrima.MaxLength = 50;
            this.tbBuscarMateriaPrima.Name = "tbBuscarMateriaPrima";
            this.tbBuscarMateriaPrima.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarMateriaPrima.TabIndex = 0;
            // 
            // btAgregarMateriaPrima
            // 
            this.btAgregarMateriaPrima.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btAgregarMateriaPrima.BackColor = System.Drawing.Color.AliceBlue;
            this.btAgregarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btAgregarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.btAgregarMateriaPrima.Name = "btAgregarMateriaPrima";
            this.btAgregarMateriaPrima.Size = new System.Drawing.Size(150, 30);
            this.btAgregarMateriaPrima.TabIndex = 0;
            this.btAgregarMateriaPrima.Text = "Agregar Materia Prima";
            this.btAgregarMateriaPrima.UseVisualStyleBackColor = false;
            this.btAgregarMateriaPrima.Click += new System.EventHandler(this.btAgregarMateriaPrima_Click);
            // 
            // tlpAddCantMatPrima
            // 
            this.tlpAddCantMatPrima.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpAddCantMatPrima.BackColor = System.Drawing.Color.LightCyan;
            this.tlpAddCantMatPrima.ColumnCount = 3;
            this.tlpAddCantMatPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 55F));
            this.tlpAddCantMatPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tlpAddCantMatPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
            this.tlpAddCantMatPrima.Location = new System.Drawing.Point(0, 0);
            this.tlpAddCantMatPrima.Margin = new System.Windows.Forms.Padding(50, 3, 50, 3);
            this.tlpAddCantMatPrima.Name = "tlpAddCantMatPrima";
            this.tlpAddCantMatPrima.RowCount = 1;
            this.tlpAddCantMatPrima.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpAddCantMatPrima.Size = new System.Drawing.Size(250, 40);
            this.tlpAddCantMatPrima.TabIndex = 0;
            // 
            // tbCant
            // 
            this.tbCant.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tbCant.Font = new System.Drawing.Font("Bodoni MT", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCant.Location = new System.Drawing.Point(0, 0);
            this.tbCant.MaxLength = 5;
            this.tbCant.Name = "tbCant";
            this.tbCant.Size = new System.Drawing.Size(80, 27);
            this.tbCant.TabIndex = 0;
            this.tbCant.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // lbCant
            // 
            this.lbCant.AutoSize = true;
            this.lbCant.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCant.Font = new System.Drawing.Font("Bodoni MT", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCant.Location = new System.Drawing.Point(0, 0);
            this.lbCant.Name = "lbCant";
            this.lbCant.Size = new System.Drawing.Size(15, 30);
            this.lbCant.TabIndex = 0;
            this.lbCant.Text = "Cant.";
            this.lbCant.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lsbMatPrima
            // 
            this.lsbMatPrima.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lsbMatPrima.Font = new System.Drawing.Font("Bodoni MT", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lsbMatPrima.FormattingEnabled = true;
            this.lsbMatPrima.ItemHeight = 20;
            this.lsbMatPrima.Location = new System.Drawing.Point(0, 0);
            this.lsbMatPrima.Name = "lsbMatPrima";
            this.lsbMatPrima.Size = new System.Drawing.Size(300, 250);
            this.lsbMatPrima.TabIndex = 0;
            // 
            // btBorrarItem
            // 
            this.btBorrarItem.BackColor = System.Drawing.Color.AliceBlue;
            this.btBorrarItem.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBorrarItem.Location = new System.Drawing.Point(0, 0);
            this.btBorrarItem.Margin = new System.Windows.Forms.Padding(125, 3, 125, 3);
            this.btBorrarItem.Name = "btBorrarItem";
            this.btBorrarItem.Size = new System.Drawing.Size(100, 25);
            this.btBorrarItem.TabIndex = 0;
            this.btBorrarItem.Text = "Borrar Item";
            this.btBorrarItem.UseVisualStyleBackColor = false;
            this.btBorrarItem.Click += new System.EventHandler(this.btBorrarItem_Click);
            // 
            // tlpLote
            // 
            this.tlpLote.BackColor = System.Drawing.Color.LightCyan;
            this.tlpLote.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpLote.ColumnCount = 2;
            this.tlpLote.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 40F));
            this.tlpLote.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 60F));
            this.tlpLote.Location = new System.Drawing.Point(0, 0);
            this.tlpLote.Name = "tlpLote";
            this.tlpLote.RowCount = 3;
            this.tlpLote.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.333F));
            this.tlpLote.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.333F));
            this.tlpLote.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.333F));
            this.tlpLote.Size = new System.Drawing.Size(350, 100);
            this.tlpLote.TabIndex = 0;
            // 
            // dtFechaVenc
            // 
            this.dtFechaVenc.CalendarFont = new System.Drawing.Font("Bodoni MT", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtFechaVenc.Location = new System.Drawing.Point(0, 0);
            this.dtFechaVenc.Name = "dtFechaVenc";
            this.dtFechaVenc.Size = new System.Drawing.Size(200, 20);
            this.dtFechaVenc.TabIndex = 0;
            // 
            // lbCod
            // 
            this.lbCod.AutoSize = true;
            this.lbCod.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCod.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCod.Location = new System.Drawing.Point(0, 0);
            this.lbCod.Name = "lbCod";
            this.lbCod.Size = new System.Drawing.Size(100, 23);
            this.lbCod.TabIndex = 0;
            this.lbCod.Text = "Código Lote";
            this.lbCod.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbCod
            // 
            this.tbCod.Font = new System.Drawing.Font("Bodoni MT", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCod.Location = new System.Drawing.Point(0, 0);
            this.tbCod.MaxLength = 10;
            this.tbCod.Name = "tbCod";
            this.tbCod.Size = new System.Drawing.Size(100, 27);
            this.tbCod.TabIndex = 0;
            // 
            // lbFechaVenc
            // 
            this.lbFechaVenc.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbFechaVenc.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbFechaVenc.Location = new System.Drawing.Point(0, 0);
            this.lbFechaVenc.Name = "lbFechaVenc";
            this.lbFechaVenc.Size = new System.Drawing.Size(100, 23);
            this.lbFechaVenc.TabIndex = 0;
            this.lbFechaVenc.Text = "Fecha Vencimiento";
            this.lbFechaVenc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            ((System.ComponentModel.ISupportInitialize)(this.dgvLotes)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private void btAgregarMateriaPrima_Click(object sender, EventArgs e)
        {

            try
            {

                if (!string.IsNullOrWhiteSpace(tbCant.Text))
                {
                    if(float.Parse(tbCant.Text) > 0)
                    {
                        int rowIndex = dgvLotes.CurrentCell.RowIndex;

                        if (!string.IsNullOrWhiteSpace(dgvLotes.Rows[rowIndex].Cells[0].Value.ToString()))
                        {
                            //Convert.ToInt32(dgvLotes.Rows[rowIndex].Cells[0].Value.ToString());
                            string strMatPrima = dgvLotes.Rows[rowIndex].Cells[0].Value.ToString() + " - " + dgvLotes.Rows[rowIndex].Cells[1].Value.ToString() + " | " + tbCant.Text + " | "+dgvLotes.Rows[rowIndex].Cells[4].Value.ToString();
                            if (!string.IsNullOrWhiteSpace(strMatPrima))
                            {
                                lsbMatPrima.Items.Add(strMatPrima);
                            }

                            tbCant.Clear();
                        }
                        else 
                        {
                            MessageBox.Show("NOHAY CELDA / MATERIA PRIMA SELECCIONADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else 
                    {
                        MessageBox.Show("NO SE INGRESO UN NUMERO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("DEBE COLOCAR CANTIDAD DE MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception - Verifique ingreso de datos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tbCant.Clear();
            }
        }

        private void btBuscarMateriaPrima_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(tbBuscarMateriaPrima.Text))
            {
                string searchValue = tbBuscarMateriaPrima.Text;


                try
                {
                    foreach (DataGridViewRow row in dgvLotes.Rows)
                    {
                        if (row.Cells[1].Value.ToString().Equals(searchValue))
                        {
                            dgvLotes.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvLotes.CurrentCell = row.Cells[0];
                            tbBuscarMateriaPrima.Clear();
                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarMateriaPrima.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO MATERIA PRIMA A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btGuardarCambios_Click(object sender, EventArgs e)
        {
            try
            {
                if (lsbMatPrima.Items.Count != 0)
                {
                    if (validarCamposLote())
                    {
                        lote = new Lote();
                        foreach (string str in lsbMatPrima.Items)
                        {
                            int index = Convert.ToInt32(str.Substring(0, str.IndexOf(" ")));

                         //   MessageBox.Show("ID LIST: " + index.ToString(), "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            MateriaPrima materiaPrima = new MateriaPrima();

                            materiaPrima.IdMateriaPrima = index;
                            
                            int cantB=str.IndexOf("| ")+2;
                            int cantE=str.LastIndexOf(" | ");
                            
                           // MessageBox.Show("STRING: "+str+ " - CANTB: " + cantB+" CANTE: "+cantE , "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            materiaPrima.Cantidad=float.Parse(str.Substring(cantB,cantE-cantB));

                            
                           // MessageBox.Show("CANTIDAD MAT: " + materiaPrima.Cantidad.ToString(), "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            lote.LstMateriaPrima.Add(materiaPrima);

                        }

                        lote.IdLote = Convert.ToInt32(lbIdLote.Text);
                        lote.FechaVencimiento = dtFechaVenc.Value.Date;
                        lote.Codigo = tbCod.Text;

                        lstLote.Add(lote);

                        //lsbListBoxLotes.Items.Add("Lote ID: "+ lote.IdLote.ToString()+" - Cod: "+lote.Codigo);
                        lsbListBoxLotes.Items.Add("Lote ID: " + lote.IdLote.ToString());

                        this.formR.Close();

                    }else
                    {
                        MessageBox.Show("DATOS MAL INGRESADOS O VACIOS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else 
                {
                    MessageBox.Show("NO SE INGRESO NINGUNA MATERIA PRIMA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show("EXCEPTION : " + ex, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }                                   
        }

        private bool validarCamposLote()
        {
            return !string.IsNullOrWhiteSpace(tbCod.Text) && !string.IsNullOrWhiteSpace(lbIdLote.Text);
        }
        

        private void btVolver_Click(object sender, EventArgs e)
        {

            this.formR.Close();
        }

        private void dgvLotes_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvLotes.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void btBorrarItem_Click(object sender, EventArgs e)
        {
             if (lsbMatPrima.Items.Count <= 0)
            {
                MessageBox.Show("NO HAY NINGÚNA MATERIA PRIMA CARGADA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if((lsbMatPrima.SelectedIndex != null)&&(lsbMatPrima.SelectedItem!=null))
                {                    
                    lsbMatPrima.Items.Remove(lsbMatPrima.SelectedItem);
                }
                else 
                {
                    MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA SELECCIONADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
    }
}
